﻿using CIS.Common.Dto.Dtos.Equipments.Commands;
using CIS.Common.Dtos.ReportDtos;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CIS.Device.PLCAction.Command
{
    [PLCAttribute("PortControlCommand")]
    public class PortControlCommandPLCAction : IPLCEvent
    {
        private Int16 portControlCommand { get; set; }
        private Int16 cassetteSequenceNo { get; set; }
        private string cassetteID { get; set; }
        private Int16 messageSequenceNo { get; set; }
        private Int16 portControlCommandReturnCode { get; set; } = 1;

        private void SetPortControlCommand(Int16 data)
        {
            portControlCommand = data;
        }

        private void SetCassetteSequenceNo(Int16 data)
        {
            cassetteSequenceNo = data;
        }

        private void SetCassetteID(string data)
        {
            cassetteID = data;
        }

        private void SetMessageSequenceNo(Int16 data)
        {
            messageSequenceNo = data;
        }

        private Int16 GetPortControlCommandReturnCode()
        {
            return portControlCommandReturnCode;
        }

        public object SendDataToServer()
        {
            var dto = new PortControlCommandDto();
            dto.PortControlCommand= portControlCommand;
            dto.CassetteSequenceNo= cassetteSequenceNo;
            dto.CassetteID= cassetteID;
            dto.MessageSequenceNo= messageSequenceNo;
            dto.PortControlCommandReturnCode= GetPortControlCommandReturnCode();
            return dto;
        }

        public void ReceiveDataFromServer(object data)
        {
            var dto = JsonConvert.DeserializeObject<PortControlCommandDto>(data.ToString());
            SetPortControlCommand(dto.PortControlCommand);
            SetCassetteSequenceNo(dto.CassetteSequenceNo);
            SetCassetteID(dto.CassetteID);
            SetMessageSequenceNo(dto.MessageSequenceNo);
        }

        public object DataReport()
        {
            PortStatusChangeReportDto report = new PortStatusChangeReportDto();
            switch (portControlCommand)
            {
                case 1://卡匣夹紧
                    report.PortStatus = 2; //1.UnloadReady 2.InUse
                    report.SourceId = "3AFS12-IND"; 
                    report.CassetteSequenceNo = cassetteSequenceNo;
                    report.CassetteID = "AN0001";
                    report.JobCountInCassette = 30;
                    report.JobExistenceSlot = "000000111111";
                    report.EventName = "PortStatusChangeReport";
                    break;
                case 2://卡匣松开
                    report.PortStatus = 1;//1.UnloadReady 2.InUse
                    report.SourceId = "3AFS12-IND"; 
                    report.CassetteSequenceNo = cassetteSequenceNo;
                    report.CassetteID = "AN0001";
                    report.JobCountInCassette = 30;
                    report.JobExistenceSlot = "000000111111";
                    report.EventName = "PortStatusChangeReport";
                    break;
            }
            return report;
        }
    }
}
